<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;
use app\common\controller\Common;
use app\common\controller\Get;

/**
 * 订单明细管理
 * Class User
 * @package app\worker\controller
 */
class OrderDetail extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'OrderDetail';

    /**
     * 订单明细列表管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->type = ['' => ' -- 全部 -- '] + Get::type();
        $this->classify = ['' => ' -- 全部 -- '] + Get::classify();
        $this->att = ['' => ' -- 全部 -- '] + Get::att();
        $this->jobs = ['' => ' -- 全部 -- '] + Get::jobs();
        $this->title = '订单明细管理';
        $query = $this->_query($this->table)
            ->alias('od')
            ->join('plan p', 'p.id = od.plan_id', 'left')
            ->join('ware_house wh', 'wh.id = p.ware_house_id', 'left')
            ->join('order o', 'o.id = od.order_id', 'left')
            ->join('user u', 'u.id = o.uid', 'left')
            ->dateBetween('od.create_at#create_at')
            ->where(['od.is_deleted' => '0'])
            ->like('u.name#user_name,u.phone#phone')
            ->equal('u.type#type,u.classify#classify,u.att#att,od.jobs#jobs')
            ->field('od.*,od.price*od.complete_count as total_price,
            wh.name as wh_name,
            p.plan_date,
            u.phone,u.name as user_name,u.type,u.classify,u.att');
        if(request()->get('order_id')){
            $query->where('od.order_id', request()->get('order_id'));
        }
        if(request()->get('plan_id')){
            $query->where('od.plan_id', request()->get('plan_id'));
        }
        if(request()->get('plan_id')){
            $query->where('od.plan_id', request()->get('plan_id'));
        }
        if(request()->get('ware_house_id')){
            $query->where('wh.id', request()->get('ware_house_id'));
        }
        if(request()->get('body_id')){
            $query->where('u.body_id', request()->get('body_id'));
        }
        if(request()->get('uid')){
            $query->where('u.id', request()->get('uid'));
        }
        if(request()->get('export')){
            $query->page(false);
        }else{
            $query->page();
        }
    }

    protected function _index_page_filter(&$data) {
        //导出数据
        if(request()->get('export')){
            foreach($data as &$v){
                $v['type'] = Get::type()[$v['type']];
                $v['classify'] = Get::classify()[$v['classify']];
                $v['att'] = Get::att()[$v['att']];
                $v['jobs'] = Get::jobs()[$v['jobs']];
            }
            $name = date('Y-m-d') . '台账';
            $head = ['ID','仓库', '运营计划', '姓名', '手机', '类型', '分类', '属性', '岗位', '完成件数', '单价金额',
             '总金额', '超时罚款总金额','商品买赔总金额','其他罚款总金额','添加时间'];
            //数据中对应的字段，用于读取相应数据：
            $keys = ['id','wh_name', 'plan_date', 'user_name', 'phone', 'type', 'classify', 'att', 'jobs', 'complete_count', 
            'price', 'total_price', 'timeout_fine_price','goods_fine_price','other_fine_price','create_at'];   
            Common::export($name, $data, $head, $keys);
        }
    }

    /**
     * 批量导出
     * @auth true
     */
    public function export(){
    }
}
